package com.yanqu.road.server.timer.washe;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.config.WasheSeasonConfig;
import com.yanqu.road.entity.activity.washe.data.WasheHistoryRank;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.logic.helper.SeasonHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.WasheProto;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

public class WasheTask extends GeneralTask {

    private long checkConfigTime = 0;
    private long checkRankTime = 0;
    private long checkSendTime = 0;
    private long checkDaySceneTime = 0;

    @Override
    public void doRun() {

        long now = System.currentTimeMillis();

        // 检查ping掉线
        WasheMgr.checkPingByTimer();

        // 检查赛季切换
        long zeroSubTime = now - DateHelper.getZeroTimeStamp(now);
        if (zeroSubTime >= DateHelper.MINUTE_MILLIONS * 3 && zeroSubTime <= DateHelper.MINUTE_MILLIONS * (23 * 60 + 58) && now > checkConfigTime) {
            checkConfigTime = now + DateHelper.SECOND_MILLIONS * 5;
            WasheConfig washeConfig = WasheMgr.getWasheConfig();
            if (washeConfig != null && washeConfig.isSeasonChange()) {
                getLOGGER().debug("handle washe season change...");
                WasheMgr.reloadActivityData();
            }
        }

        // 检查榜单名次
        if (now > checkRankTime) { // 检测间隔时间
            checkRankTime = now + DateHelper.MINUTE_MILLIONS;
            WasheConfig washeConfig = WasheMgr.getWasheConfig();
            if (washeConfig != null) { // 有瓦舍
                WasheSeasonConfig washeSeasonConfig = washeConfig.getWasheSeasonConfig();
                if (washeSeasonConfig != null && washeSeasonConfig.getSeasonId() > WasheConfig.SEASON_ID_TEST) { // 有赛季
                    if (washeSeasonConfig.getEndTime() + DateHelper.MINUTE_MILLIONS * 5 <= now && now < washeSeasonConfig.getEndShowTime()) { // 领奖期
                        // 需要拉哪些榜单
                        for (eSeasonRewardType rewardType : eSeasonRewardType.values()) {
                            if (!rewardType.isRankType()) {
                                continue;
                            }
                            int rankType = rewardType.getIntValue();
                            if (!WasheMgr.getHistoryRankMap().containsKey(washeSeasonConfig.getSeasonId()) || !WasheMgr.getHistoryRankMap().get(washeSeasonConfig.getSeasonId()).containsKey(rankType)) { // 没数据
                                WasheProto.CrossWasheGetRankRecordReqMsg.Builder builder = WasheProto.CrossWasheGetRankRecordReqMsg.newBuilder();
                                builder.setSeasonId(washeSeasonConfig.getSeasonId());
                                builder.setRangeKey(SeasonHelper.getWasheRangeKey(washeConfig.getActivityInfo().getParam6()));
                                builder.setRankType(rankType);
                                YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_WASHE_GET_RANK_RECORD_FROM_GAME, builder);
                                GamePlayerMgr.sendPacket(0L, message);
                            }
                        }
                    }
                }
            }
        }

        // 检查自动发奖（不需要了，如果后续开起来，可能会把之前没补发的都补发了...）
        /*
        if (now > checkSendTime) { // 检测间隔时间
            checkSendTime = now + DateHelper.MINUTE_MILLIONS;
            for (WasheHistoryRank historyRank : WasheMgr.getHistoryRankMap().values()) {
                if (!historyRank.isAutoSendReward()) {
                    WasheMgr.tryAutoSendReward(historyRank.getSeasonId());
                }
            }
        }*/

        // 检查今日玩法
        if (now > checkDaySceneTime) {
            checkDaySceneTime = now + DateHelper.SECOND_MILLIONS * 5;
            try {
                WasheMgr.checkTodayScene();
            } catch (Exception e) {
                getLOGGER().error("error : ", e);
            }
        }

    }
}
